<!-- 预开票关联报名信息 -->
<template>
  <el-dialog
    :title="title"
    :visible.sync="visible"
    width="1500px"
    @close="reset"
    append-to-body
    :close-on-click-modal="false"
  >
    <div class="relation-container">
      <!-- 左侧树形菜单 -->
      <div class="tree-panel">
        <splitpanes
          :horizontal="this.$store.getters.device === 'mobile'"
          class="default-theme"
          style="height: 100vh"
        >
          <pane size="10">
            <div class="head-container">
              <el-input
                v-model="projectName"
                placeholder="请输入培训项目"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px; width: 70%"
              />
            </div>
            <div class="head-container">
              <el-tree
                :data="projectOptions"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                ref="tree"
                node-key="id"
                default-expand-all
                highlight-current
                @node-click="handleNodeClick"
              />
            </div>
          </pane>
        </splitpanes>
      </div>

      <!-- 右侧搜索表单 -->
      <div class="content-panel">
        <el-col>
          <el-form :model="queryParams" inline label-width="80px">
            <el-col>
              <el-form-item label="姓名">
                <el-input
                  v-model="queryParams.studentName"
                  placeholder="请输入姓名"
                  style="100px"
                  clearable
                />
              </el-form-item>
              <el-form-item label="证件编号">
                <el-input
                  v-model="queryParams.idNumber"
                  placeholder="请输入证件编号"
                  style="100px"
                  clearable
                />
              </el-form-item>
              <el-form-item label="单位名称">
                <el-input
                  v-model="queryParams.unitName"
                  placeholder="请输入单位名称"
                  style="100px"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col>
              <el-form-item label="手机号码">
                <el-input
                  v-model="queryParams.phonenumber"
                  placeholder="请输入手机号码"
                  clearable
                />
              </el-form-item>
              <el-form-item label="缴费状态">
                <el-select
                  v-model="queryParams.paymentStatus"
                  placeholder="请选择缴费状态"
                >
                  <el-option
                    v-for="(label, value) in dict.payment_status"
                    :key="value"
                    :label="label"
                    :value="value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleSearch">搜索</el-button>
                <el-button @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-col>
          </el-form>

          <!-- 数据表格 -->
          <el-table
            :data="list"
            style="width: 100%"
            border
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column prop="studentName" label="姓名" align="center" />
            <el-table-column prop="idNumber" label="证件编号" align="center" />
            <el-table-column prop="phonenumber" label="电话" align="center" />
            <el-table-column prop="unitName" label="单位名称" align="center" />
            <el-table-column prop="contact" label="联系人" align="center" />
            <el-table-column
              prop="unitPhonenumber"
              label="联系人电话"
              align="center"
            />
            <el-table-column prop="projectSum" label="费用" align="center" />
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="confirm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { projectTreeSelect } from "@/api/training/projects";
import { Splitpanes, Pane } from "splitpanes";
import "splitpanes/dist/splitpanes.css";
import DictTag from "@/components/DictTag";
import { registrationList } from "@/api/training/registration";

export default {
  name: "PreInvoiceRelationDialog",
  components: {
    Splitpanes,
    Pane,
    DictTag,
  },
  props: {
    title: {
      type: String,
      default: "预开票关联报名信息",
    },
    dict: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      visible: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        studentName: "",
        idNumber: "",
        unitName: "",
        phonenumber: "",
        paymentStatus: "",
        projectId: undefined,
      },
      list: [],
      total: 0,
      selectedRows: [],

      //树形结构数据
      // 所有部门树选项
      projectOptions: null,
      projectName: "",
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  created() {
    this.getProjectTree();
  },
  methods: {
    open() {
      this.visible = true;
      this.reset();
    },
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        studentName: "",
        idNumber: "",
        unitName: "",
        phonenumber: "",
        paymentStatus: "",
      };
      this.list = [];
      this.total = 0;
      this.selectedRows = [];
      this.getList();
    },
    resetQuery() {
      this.reset();
    },
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    handleSelectionChange(selection) {
      this.selectedRows = selection;
    },
    getList() {
      registrationList(this.queryParams).then((response) => {
        this.list = response.rows;
        this.total = response.total;
      });
    },
    confirm() {
      if (this.selectedRows.length === 0) {
        this.$message.warning("请至少选择一条记录");
        return;
      }
      this.$emit("confirm", this.selectedRows);
      this.visible = false;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.projectId = data.id;
      this.queryParams.pageNum = 1;
      this.getList(this.queryParams);
      //查询人员list
    },
    // 树节点过滤方法
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 获取树形数据
    getProjectTree(val) {
      var params = { projectName: val };
      params.projectId = this.queryParams.projectId;
      projectTreeSelect(params).then((response) => {
        this.projectOptions = response.data;
        this.enabledProjectOptions = this.filterDisabledDept(
          JSON.parse(JSON.stringify(response.data))
        );
      });
    },
  },
  watch: {
    // 根据名称筛选部门树
    projectName(val) {
      this.$refs.tree.filter(val);
      this.getProjectTree(val);
    },
  },
};
</script>

<style scoped>
.relation-container {
  display: flex;
  height: 600px;
}
.tree-panel {
  width: 300px;
  border-right: 1px solid #ebeef5;
  overflow-y: auto;
}
.content-panel {
  flex: 1;
  padding: 20px;
}
</style>